

function firstChild(oParent){							//第一个子元素;
	return oParent.firstElementChild || oParent.firstChild;
}
function lastChild(oParent){							//最后一个子元素;
	return oParent.lastElementChild || oParent.lastChild;	
}
function prevNode(obj){									//上一个兄弟节点;
	return obj.previousElementSibiling || previousSibling;
}
function nextNode(obj){									//下一个兄弟节点;
	return obj.nextElementSibling || nextElementSibling;
}
function hasChild(obj){									//有无子节点
	var ele = obj.getElementsByTagName('*');
	if(!ele.length){
		return false;
	}
	return true;
}
function viewH(){										//可视区高度;
	return document.documentElement.clientHeight;	
}
function viewW(){										//可视区宽度;
	return document.documentElement.clientWidth;	
}
function scrollY(){										//滚过的高度;
	return document.body.scrollTop || document.documentElement.scrollTop;
}
function scrollX(){										//滚过的宽度;
	return document.body.scrollLeft || document.documentElement.scrollLeft;	
}
function scrollH(){										//页面内容高度;
	return document.body.scrollHeight;	
}
function scrollW(){										//页面宽内容度;
	return document.body.scrollWidth;	
}
function $$(str,oParent){								//getElement
	var id = /^#[\w\d]+/;
	var tag = /^<\w{1,}>$/;		
	if(id.test(str)){
		return (oParent||document).getElementById(str.substring(1));	
	}
	if(tag.test(str)){
		return (oParent||document).getElementsByTagName(str.substring(1,(str.length-1)));	
	}	
}
function browInfo(){
	var info = navigator.userAgent;
	if(info.indexOf('MSIE 8') != -1){
		return 'IE8';	
	}
	if(info.indexOf('MSIE 7') != -1){
		return 'IE7';	
	}
	if(info.indexOf('MSIE 6') != -1){
		return 'IE6';	
	}else{
		return 'nace';	
	}
}
function getByClass(oParent,sClass){					//通过class获取元素;
	var iResult = [];
	var aEle = oParent.getElementsByTagName('*');
	for(var i=0;i<aEle.length;i++){
		var aClass = aEle[i].className.split(' ');
		for(var j = 0;j<aClass.length;j++){
			if(aClass[j] == sClass){
				iResult.push(aEle[i]);
			}
		}
	}
	return iResult;
};
function addClass(obj,sClass){							//添加className;
	var aClass = obj.className.split(' ');
	for(var i=0;i<aClass.length;i++){
		if(aClass[i] == sClass){
			return;	
		}
	}
	if(!obj.className){
		obj.className = sClass;
		return;	
	}
	obj.className +=' ' + sClass;		
}
function removeClass(obj,sClass){						//删除className;
	if(obj.className){
 		var aClass = obj.className.split(' ');
		for(var i=0;i<aClass.length;i++){
			if(aClass[i] == sClass ){
				aClass.splice(i,1);
				obj.className = aClass.join(' ');
				return;	
			}
		}
	}
}
function addEvent(obj,sEv,fn){							//绑定事件;
	if(obj.attachEvent){
		obj.attachEvent('on'+sEv,function (){
			fn.call(obj);
		});
		return;
	}
	obj.addEventListener(sEv,fn,false);
}
function delEvent(obj,sEv,fn){							//移除事件;
	if(obj.detachEvent){
		obj.detachEvent('on'+sEv,fn);
		return;
	}
	obj.removeEventListener(sEv,fn,false);
}
function getAttribute(obj,attr){						//获取属性;
	if(attr == 'class'|| attr == 'className'){
		return obj.getAttribute('class')||obj.getAttribute('className');
	}
	return obj.getAttribute(attr);
}
function setStyle(obj,jAttr){//设置样式;
	console.log(obj);
	for(var attr in jAttr){
		if(attr == 'opacity'){
			obj.style[attr] = jAttr[attr];
			obj.style.filter = 'alpha(opacity:'+jAttr[attr]*100+')';
			return;
		}
		if(attr == 'z-index'){
			obj.style.zIndex = 	jAttr[attr];
			return;
		}
		obj.style[attr] = jAttr[attr];	
	}
}
function getStyle(obj,attr){							//获取样式;
	if(obj.currentStyle){
		return obj.currentStyle[attr];	
	}
	return getComputedStyle(obj,false)[attr];
}
function isCollision (obj1,obj2){						//碰撞检测
	var L1= obj1.offsetLeft,
		R1= obj1.offsetLeft + obj1.offsetWidth,
		T1= obj1.offsetLeft,
		B1= obj1.offsetLeft + obj1.offsetHeight,
		
		L2= obj2.offsetLeft,
		R2= obj2.offsetLeft + obj2.offsetWidth,
		T2= obj2.offsetLeft,
		B2= obj2.offsetLeft + obj2.offsetHeight;
		
	if(R1<L2||L1>R2||T1>B2||B1<T2){
		return false;	
	}
	else{
		return true;
	}
}
function distance (obj1,obj2){							//距离
	var a = obj1.offsetLeft - obj2.offsetLeft;
	var b = obj1.offsetTop - obj2.offsetTop;
	return Math.sqrt(a*a+b*b);
}
function drag(obj){
	obj.onmousedown = function (ev){
		var ev = ev||event;	
		var X = ev.clientX - obj.offsetLeft;
		var Y = ev.clientY - obj.offsetTop;
		if(ev.setCapture){
			ev.setCapture();
		}
		document.onmousemove = function (ev){
			var ev = ev||event;
			var iL = ev.clientX - X;
			var iT = ev.clientY - Y;

			iL = iL<0 ? 0 : iL>viewW()-obj.offsetWidth ? viewW()-obj.offsetWidth:iL;
			iT = iT<0 ? 0 : iT>viewH()-obj.offsetHeight ? viewH()-obj.offsetHeight:iT;

			obj.style.top = iT +'px';
			obj.style.left = iL +'px';
		};
		document.onmouseup = function (){
			document.onmousemove = document.onmouseup =  null;
			if(ev.releaseCapture){
				ev.releaseCapture();
			}
		}
		
		return false;
	};
}
function fnMove(obj,json,fn){		//页面运动效果
	clearInterval(obj.timer);

	obj.timer = setInterval(function(){	
		var flag=true;
		for(var attr in json){	
			var iCur = getStyle(obj,attr);
			if( attr == 'opacity'){
				iCur = Math.round(iCur*100);
			}else{
				 iCur = parseInt(iCur);
			}
			var iSpeed = (Number(json[attr])-iCur)/8;
			iSpeed = iSpeed >0 ? Math.ceil(iSpeed):Math.floor(iSpeed);
			
			if(iCur != Number(json[attr])){
				flag=false;
			}
			if( attr == 'opacity'){
				obj.style.filter = 'alpha(opacity:'+(iCur +iSpeed)+')';
				obj.style.opacity = (iCur +iSpeed)/100;
			}else{
				obj.style[attr] = (iCur+iSpeed)+'px';
			}
		}
		if(flag){
				clearInterval(obj.timer);
				obj.timer = null;
			if(fn){
				fn();
			}
		}
	},0)
}
function setBg(oBg){
	var oMenu = $$('#bgWindowMenu');
	var oClose = $$('#bgclose');
	var bgImg1 = $$('#deskbg1');
	var aImg = $$('<img>',oBg);
	var iNow = 0;
	
	// fnDrag(oMenu,oBg);             					//窗口移动
	if(getStyle(oBg,'display') == 'none'){
		oBg.style.display = 'block';
		oBg.style.right  = '-8px';
		oBg.style.height = '100%';
		oBg.style.zIndex = zh_winzIndex++;
	}else{
		oBg.style.display = 'none';
	}
	oClose.onclick = function(ev){
		var ev = ev||event;
		oBg.style.display = 'none';
		ev.cancelBubble = true;
		return false;
	};
	for(var i=0;i<aImg.length;i++){/*换背景图片*/
		aImg[i].onclick = function (){
			localStorage.clear('deskBg');
			bgImg1.src = this.getAttribute('src');		
			localStorage.deskBg = this.getAttribute('src');
		};	
	}
}
function fnDrag(obj1,obj2){
	obj1.onmousedown = function(ev){
		var ev = ev||event;
		var disX = ev.clientX - obj2.offsetLeft;
		var disY = ev.clientY - obj2.offsetTop;
		if(obj1.setCapture){
			obj1.setCapture();	
		}
		obj2.style.zIndex = zh_winzIndex++;
		document.onmousemove = function (ev){
			var ev = ev||event;	
			var iL = ev.clientX - disX;
			var iT = ev.clientY - disY;
			if(iT<30){
				iT = 30;	
			}
			if(iT>(viewH())){
				iT = viewH();	
			}
			obj2.style.left = iL+'px';
			obj2.style.top = iT+'px';
			document.onmouseup = function (){
				document.onmousemove = document.onmouseup = null;
				if(obj1.releaseCapture){
					obj1.releaseCapture();
				}
			}
		};
		ev.cancelBubble = true;
		return false;
	};	
}
function createElem(element){							//创建element元素
	return document.createElement(element);
}
function drop(ev){										//拖放，放置目标源
	ev.preventDefault();
	var data = ev.dataTransfer.getData("Text");	//获得操作APP本身元素
	var newAddElemt = document.getElementById(data);	//需要插入的元素
	if(ev.target.parentNode.parentNode.parentNode.className=='deskContent clear'){	//放到图标的上一级
		let parent = ev.target.parentNode.parentNode;	//farther node
		let nextChild = ev.target.parentNode.nextElementSibling;	//next node
		let jsonAll = ev.srcElement.parentNode.parentNode.children;
		var sum = '';
		for(let i=0; i<jsonAll.length;i++){
			try{
				var a =jsonAll[i].json.pictureSize;
			}catch(e){

			}
			if(a){
				sum+=a;
			}else{
				sum+='b';
			}
		}
		console.log(sum);
		if(fullFill(sum)){
			//这里我们做一些逻辑，具体是什么逻辑呢，关于如果这个是本区域的APP移动的话，那就不添加
			//任何的元素，只是把位置移动下。九宫格满了后，如果是本区域里移动的APP是可以移动的。
			// console.log(ev.target.parentNode.parentNode.parentNode.id);//本区域的定位
			// console.log(newAddElemt.parentNode.id);//新移动的APP定位
			if(ev.target.parentNode.parentNode.id==newAddElemt.parentNode.id){
				parent.insertBefore(newAddElemt,nextChild);
			}else{
				alert('九宫格已满,请调整后，再把应用放到此区域1。');
			}
			
		}else{
			parent.insertBefore(newAddElemt,nextChild);
			var json = ev.srcElement.parentNode.parentNode.nextElementSibling.json;
		}
		let regions = ev.srcElement.parentNode.json.region;
		fnAxios(json,regions);
	}
	if(ev.target.parentNode.parentNode.parentNode.className == 'deskIcon'){	//放到图标里
		let parent = ev.target.parentNode.parentNode.parentNode;	//farther node
		let nextChild = ev.target.parentNode.parentNode.nextElementSibling;	//next node
		let jsonAll = ev.srcElement.parentNode.parentNode.parentNode.children;
		var sum = '';
		for(let i=0; i<jsonAll.length;i++){
			try{
				var a =jsonAll[i].json.pictureSize;
			}catch(e){

			}
			if(a){
				sum+=a;
			}else{
				sum+='b';
			}
		}
		console.log(sum);
		if(fullFill(sum)){
			//这里我们做一些逻辑，具体是什么逻辑呢，关于如果这个是本区域的APP移动的话，那就不添加
			//任何的元素，只是把位置移动下。九宫格满了后，如果是本区域里移动的APP是可以移动的。
			// console.log(ev.target.parentNode.parentNode.parentNode.id);//本区域的定位
			// console.log(newAddElemt.parentNode.id);//新移动的APP定位
			if(ev.target.parentNode.parentNode.parentNode.id==newAddElemt.parentNode.id){
				parent.insertBefore(newAddElemt,nextChild);
			}else{
				alert('九宫格已满,请调整后，再把应用放到此区域2。');
			}
			
		}else{
			parent.insertBefore(newAddElemt,nextChild);
			var json = ev.srcElement.parentNode.parentNode.nextElementSibling.json;
		}
		let regions = ev.srcElement.parentNode.parentNode.json.region;
		fnAxios(json,regions);
	}
	if(ev.target.className == 'deskIcon'){              //容器里放图标应用
		let jsonAll = ev.srcElement.children;
		var sum = '';
		for(let i=0; i<jsonAll.length;i++){
			try{
				var a =jsonAll[i].json.pictureSize;
			}catch(e){

			}
			if(a){
				sum+=a;
			}else{
				sum+='b';
			}
		}
		console.log(sum);
		console.log(jsonAll.length);
		if(fullFill(sum)){
			alert('九宫格已满,请调整后，再把应用放到此区域。');
		}else{
			ev.target.appendChild(newAddElemt);
			var json = 	ev.srcElement.lastChild.json;
		}
		let regions = ev.target.id;
		fnAxios(json,regions);
	}	
}
function drag(ev){										//拖放
	ev.dataTransfer.setData("Text",ev.target.id);
}
function allDrop(ev){									//拖放，放置目标源设置
	ev.preventDefault();
}
function wanLi(){                               		//农历年
    let wanLiObj = {},
        D = new Date(),
        yy = D.getFullYear(),
        mm = D.getMonth() + 1,
        dd = D.getDate(),
        today = new Date(),
        d = new initArray("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    var madd = new Array(12);
    var tgString = "甲乙丙丁戊己庚辛壬癸";
    var dzString = "子丑寅卯辰巳午未申酉戌亥";
    var numString = "一二三四五六七八九十";
    var monString = "正二三四五六七八九十冬腊";
    var weekString = "日一二三四五六";
    var sx = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
    var cYear, cMonth, cDay, TheDate;
    CalendarData = new Array(0xA4B, 0x5164B, 0x6A5, 0x6D4, 0x415B5, 0x2B6, 0x957, 0x2092F, 0x497, 0x60C96, 0xD4A, 0xEA5, 0x50DA9, 0x5AD, 0x2B6, 0x3126E, 0x92E, 0x7192D, 0xC95, 0xD4A, 0x61B4A, 0xB55, 0x56A, 0x4155B, 0x25D, 0x92D, 0x2192B, 0xA95, 0x71695, 0x6CA, 0xB55, 0x50AB5, 0x4DA, 0xA5B, 0x30A57, 0x52B, 0x8152A, 0xE95, 0x6AA, 0x615AA, 0xAB5, 0x4B6, 0x414AE, 0xA57, 0x526, 0x31D26, 0xD95, 0x70B55, 0x56A, 0x96D, 0x5095D, 0x4AD, 0xA4D, 0x41A4D, 0xD25, 0x81AA5, 0xB54, 0xB6A, 0x612DA, 0x95B, 0x49B, 0x41497, 0xA4B, 0xA164B, 0x6A5, 0x6D4, 0x615B4, 0xAB6, 0x957, 0x5092F, 0x497, 0x64B, 0x30D4A, 0xEA5, 0x80D65, 0x5AC, 0xAB6, 0x5126D, 0x92E, 0xC96, 0x41A95, 0xD4A, 0xDA5, 0x20B55, 0x56A, 0x7155B, 0x25D, 0x92D, 0x5192B, 0xA95, 0xB4A, 0x416AA, 0xAD5, 0x90AB5, 0x4BA, 0xA5B, 0x60A57, 0x52B, 0xA93, 0x40E95);
    madd[0] = 0;
    madd[1] = 31;
    madd[2] = 59;
    madd[3] = 90;
    madd[4] = 120;
    madd[5] = 151;
    madd[6] = 181;
    madd[7] = 212;
    madd[8] = 243;
    madd[9] = 273;
    madd[10] = 304;
    madd[11] = 334;
    function initArray() {
        this.length = initArray.arguments.length
        for (var i = 0; i < this.length; i++) this[i + 1] = initArray.arguments[i]
    }
    function GetLunarDay(solarYear, solarMonth, solarDay) {
        if (solarYear < 1921 || solarYear > 2020) {
            return "";
        } else {
            solarMonth = (parseInt(solarMonth) > 0) ? (solarMonth - 1) : 11;
            e2c(solarYear, solarMonth, solarDay);
            return GetcDateString();
        }
    }
    function e2c() {
        var TheDate = (arguments.length != 3) ? new Date() : new Date(arguments[0], arguments[1], arguments[2]);
        var total, m, n, k;
        var isEnd = false;
        var tmp = TheDate.getYear();
        if (tmp < 1900) {
            tmp += 1900;
        }
        total = (tmp - 1921) * 365 + Math.floor((tmp - 1921) / 4) + madd[TheDate.getMonth()] + TheDate.getDate() - 38;
        if (TheDate.getYear() % 4 == 0 && TheDate.getMonth() > 1) {
            total++;
        }
        for (m = 0;; m++) {
            k = (CalendarData[m] < 0xfff) ? 11 : 12;
            for (n = k; n >= 0; n--) {
                if (total <= 29 + GetBit(CalendarData[m], n)) {
                    isEnd = true;
                    break;
                }
                total = total - 29 - GetBit(CalendarData[m], n);
            }
            if (isEnd) break;
        }
        cYear = 1921 + m;
        cMonth = k - n + 1;
        cDay = total;
        if (k == 12) {
            if (cMonth == Math.floor(CalendarData[m] / 0x10000) + 1) {
                cMonth = 1 - cMonth;
            }
            if (cMonth > Math.floor(CalendarData[m] / 0x10000) + 1) {
                cMonth--;
            }
        }
    }
    function GetBit(m, n) {
        return (m >> n) & 1;
    }
    function GetcDateString() {
        var tmp = "";
        if (cMonth < 1) {
            tmp += "(闰)";
            tmp += monString.charAt( - cMonth - 1);
        } else {
            tmp += monString.charAt(cMonth - 1);
        }
        tmp += "月";
        tmp += (cDay < 11) ? "初": ((cDay < 20) ? "十": ((cDay < 30) ? "廿": "三十"));
        if (cDay % 10 != 0 || cDay == 10) {
            tmp += numString.charAt((cDay - 1) % 10);
        }
        return tmp;
    }
    wanLiObj.todayRi = today.getDate();
    wanLiObj.weekDisplay = d[today.getDay() + 1];
    wanLiObj.lunarDay = GetLunarDay(yy, mm, dd);
    return wanLiObj;
}
//封装axios请求
function fnAxios(option,regions){
	axios.post(zuulHost+'/m_manage/userAppMenu/update?access_token='+accessToken,{
		"id":option.id,
		"appCode": option.appCode,
		"pos": option.pos,
		"title": option.title,
		"src": option.src,
		"webSrc": option.webSrc,
		"iwidth": 1300,
		"iheight": 500,
		"ileft": 110,
		"region": regions,
		"pictureSize": option.pictureSize,
		"desktop": 1
		})
		.then(function(res){
			console.log(res);
		})
		.catch(function(err){
		console.log(err);
	});
}
//九宫格已满的函数
function fullFill(sum){
	return sum == '411111' || sum == '141111' || sum == '114111' || sum == '111411' || sum == '111141' || sum == '111114'  //一大五小
	|| sum == '42111' || sum == '41211' || sum =='41121' || sum =='41112' || sum =='24111' || sum =='21411' || sum =='21141' || sum =='21114' || sum =='11142' || sum =='11124' || sum =='11124' || sum =='11241' || sum =='11214' || sum =='14211' || sum =='14121' || sum =='14112' || sum =='12114' || sum =='12141' || sum =='12411' 	//一大一中三小
	|| sum == '21111111' || sum == '12111111' || sum == '11211111' || sum == '11121111' || sum == '11112111' || sum == '11111211' || sum == '11111121' || sum == '11111112'	//一中七小
	|| sum == '2211111' || sum == '2121111' || sum == '2112111' || sum == '2111211' || sum == '2111121' || sum == '2111112'  	//二中五小
	|| sum == '2211111' || sum == '2121111' || sum == '2112111' || sum == '2111211' || sum == '2111121' || sum == '211112'	//三中三小
	|| sum == '111111111'	//九小
}

/*代办事项*/
function daiBai(val='2'){
	this.daiBaiNum = getDaiBaiNum();
}

function  getDaiBaiNum(){/*获取待办数字 */
	let num = 0;
	$.ajax({
		type:"post",
		url:developURL+richengDK+"task/count?access_token="+localStorage.token,
		async:false,
		success:function(data){
//			alert(data);
//			return data;
			num = data.count;
		}
	});
	return num;
}
//
//daiBai.prototype={
//	
//}

	
function changeLeftIcon(val){/*更换左侧导航栏的图标*/
		for (let i = 0; i < zh_leftIcon.length; i++) {
			if(zh_leftIcon[i].id == val){
				$('#leftBar'+val).find('img').attr('src', zh_leftIcon[i].chooseSrc);
			}else{
				$('#leftBar'+zh_leftIcon[i].id).find('img').attr('src', zh_leftIcon[i].src);
			}
		}
}


function openDaiBan(){/*只打开待办*/
//	let daiban=$('div[secondid="8d6e99ae1a93470a8ee1f7c773e2ba53"]');/*固定一个，待办的appcode*/
	let daiban = $("#desk13828");
	console.log(daiban);
	daiban.mousedown();
	daiban.mouseup();
	daiban.click();
	console.log($("#desk1378"));
}



function out(){/*退出*/
//	if(confirm("确认退出?")){
//		
//	}
	layer.confirm('确定退出?',{icon: 3, title:false, closeBtn:0},function(index){
	  //do something
	  localStorage.clear();		
		window.location.href="./index.html";  
	  layer.close(index);
	});  
}

function layerDiyMyself(){/*个人主页的弹窗*/
	layer.open({
      type: 2,
      title: false,
      moveOut: true,
      closeBtn:1, 
      maxmin: false,/*允许最大化*/
      shadeClose: true, //点击遮罩关闭层
      area : ['460px' , '590px'],
      content: './other/myself/index.html',
    });
}
